#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    string longestPalindrome(string s)
    {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));

        for (int i = 0; i < n; i++)
        {
            dp[i][i] = 1;
        }

        int left = 0, right = 0;
        for (int z = 1; z < n; z++)
        {
            for (int i = 0, j = i + z; i < n && j < n; i++, j++)
            {
                if (i + 1 <= j - 1)
                {
                    dp[i][j] = (dp[i + 1][j - 1] == 1) && (s[i] == s[j]);
                }
                else
                {
                    dp[i][j] = s[i] == s[j];
                }
                if (dp[i][j])
                {
                    left = i;
                    right = j;
                }
            }
        }
        return s.substr(left, right - left + 1);
    }
};


class Solution {
public:
    string longestPalindrome(string s)
    {
        int n = s.size();
        int left = 0, right = 0;
        for (int i = 0; i < n; i++)
        {
            int l = i, r = i;
            while (l >= 0 && r < n)
            {
                if (s[l] != s[r])
                    break;
                l--;
                r++;
            }
            if ((r - 1) - (l + 1) > right - left)
            {
                left = l + 1;
                right = r - 1;
            }

            l = i, r = i + 1;
            while (l >= 0 && r < n)
            {
                if (s[l] != s[r])
                    break;
                l--;
                r++;
            }
            if ((r - 1) - (l + 1) > right - left)
            {
                left = l + 1;
                right = r - 1;
            }
        }

        return s.substr(left, right - left + 1);
    }
};